//
//  FMPersonVC.m
//  FunKey
//  Created by FC on 15/9/14.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "FMPersonVC.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"
#import "AppDelegate.h"
@interface FMPersonVC ()

@property (retain ,nonatomic) MBProgressHUD *MBP;




@end

@implementation FMPersonVC

-(void)dealloc
{
    [_tableView release];

    [super dealloc];
    
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
      
        self.array = [NSMutableArray array];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.name;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"33FFFF"];
    
    self.hidesBottomBarWhenPushed = YES;//取消tabBar
   
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImage *image2 = [UIImage imageNamed:@"VList_Left@2x.png"];
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    UIImage *image = [UIImage imageNamed:@"player2.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(player1)];
    
    
    //菊花(小黄人)
    [GiFHUD setGifWithImageName:@"xiaohuangren.gif"];
    [GiFHUD show];
    [self addHeader];//下拉刷新
    [self createTableView];
    [self getMassage];
   
    
    
}


-(void)createTableView
{
    
    self.view.backgroundColor = [UIColor blackColor];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, MYWIDTH, 618 * HEIGHT)];
    [self.view addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorColor = [UIColor grayColor];
    self.tableView.backgroundColor = [UIColor blackColor];
    self.tableView.showsVerticalScrollIndicator = NO;//取消纵向滑条
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//取消cell之间的线
   [_tableView release];
    
}


-(void)addHeader
{
    
    [self.tableView addHeaderWithCallback:^{
        
        
        [self getMassage];
        
        
        [self.tableView headerBeginRefreshing];
    }];
    
    [self.tableView headerEndRefreshing];
    
    
}

-(void)getMassage
{
  
    NSString *string =[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/30?device=iPhone",self.myId];
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
      
        NSMutableDictionary *dic = responseObject;
        NSMutableDictionary *dic2 = [dic objectForKey:@"tracks"];
        NSMutableArray *array = [dic2 objectForKey:@"list"];

        for (NSMutableDictionary *smallDic in array) {
            
            FMPersonModel *model = [[FMPersonModel alloc]init];
            
            [model setValuesForKeysWithDictionary:smallDic];
            
            [self.array addObject:model];
            [model release];
            
        }

        //刷新tableView
        [self.tableView reloadData];
        [GiFHUD dismiss];//菊花隐藏
        [self.tableView  headerEndRefreshing];//结束下拉
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        //[GiFHUD dismiss];//菊花隐藏
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您的网络不给力,请检查网络设置" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        
    }];
}


//区数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}

//每个区cell的数量
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.array.count ;
    
}


//每个cell上内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellName = @"myCell";
    FMPersonCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell == nil) {
        cell = [[FMPersonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
        
    }
 
        FMPersonModel *model = [self.array objectAtIndex:indexPath.row];
        cell.model = model;
    cell.backgroundColor = [UIColor blackColor];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消点击效果
    
    return  cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 110*OffHeight;
    
}

#pragma mark -- 点击跳入播放页面

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FMPlayerVC *player = [FMPlayerVC shareHandleController];
    
    FMPersonModel *model = [self.array objectAtIndex:indexPath.row];
    
    player.title = model.title;
    player.trackId = model.trackId;
    NSLog(@"trackId == %@",player.trackId);
    
    player.playUrl = model.playUrl32;
     player.Audio = [NSMutableArray arrayWithArray:self.array];
    
    player.indexPath = indexPath;

    

    [self.navigationController pushViewController:player animated:YES];
    
    
}


#pragma mark ---右上按钮进入播放页面
-(void)player1
{
    
    FMPlayerVC *playerVC = [FMPlayerVC shareHandleController];
    
    playerVC.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:playerVC animated:YES];
}




-(void)back
{
    [GiFHUD dismiss];//菊花隐藏
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
